home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / YoutubeModel.as < prev   
Text File  |  2008-11-04  |  7KB  |  222 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import flash.display.Loader;
  7.    import flash.events.ErrorEvent;
  8.    import flash.events.IOErrorEvent;
  9.    import flash.events.StatusEvent;
  10.    import flash.net.LocalConnection;
  11.    import flash.net.URLRequest;
  12.    
  13.    public class YoutubeModel implements ModelInterface
  14.    {
  15.        
  16.       
  17.       private var connected:Boolean;
  18.       
  19.       private var loader:Loader;
  20.       
  21.       private var metasent:Boolean;
  22.       
  23.       private var model:Model;
  24.       
  25.       private var outgoing:LocalConnection;
  26.       
  27.       private var inbound:LocalConnection;
  28.       
  29.       private var loading:Boolean;
  30.       
  31.       public function YoutubeModel(param1:Model)
  32.       {
  33.          var url:* = undefined;
  34.          var ytb:* = undefined;
  35.          var mod:Model = param1;
  36.          super();
  37.          model = mod;
  38.          outgoing = new LocalConnection();
  39.          outgoing.allowDomain("*");
  40.          outgoing.allowInsecureDomain("*");
  41.          outgoing.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange);
  42.          inbound = new LocalConnection();
  43.          inbound.allowDomain("*");
  44.          inbound.allowInsecureDomain("*");
  45.          inbound.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange);
  46.          inbound.client = this;
  47.          try
  48.          {
  49.             inbound.connect("_AS2_to_AS3");
  50.             connected = true;
  51.          }
  52.          catch(err:Error)
  53.          {
  54.             stop();
  55.             model.sendEvent(ModelEvent.ERROR,{"message":"Cannot connect to Youtube. Only one YouTube connection per computer can be made!"});
  56.          }
  57.          loader = new Loader();
  58.          loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  59.          url = model.skin.loaderInfo.url;
  60.          ytb = url.substr(0,url.lastIndexOf("/") + 1) + "yt.swf";
  61.          loader.load(new URLRequest(ytb));
  62.       }
  63.       
  64.       public function onSwfLoadComplete() : void
  65.       {
  66.          outgoing.send("_AS3_to_AS2","setSize",320,240);
  67.          if(model.config["mute"] == true)
  68.          {
  69.             volume(0);
  70.          }
  71.          else
  72.          {
  73.             volume(model.config["volume"]);
  74.          }
  75.          if(loading)
  76.          {
  77.             load();
  78.          }
  79.       }
  80.       
  81.       public function onLocalConnectionStatusChange(param1:StatusEvent) : void
  82.       {
  83.       }
  84.       
  85.       public function onLoadChange(param1:Number, param2:Number, param3:Number) : void
  86.       {
  87.          model.sendEvent(ModelEvent.LOADED,{
  88.             "loaded":param1,
  89.             "total":param2,
  90.             "offset":param3
  91.          });
  92.       }
  93.       
  94.       public function seek(param1:Number) : void
  95.       {
  96.          outgoing.send("_AS3_to_AS2","seekTo",param1);
  97.          play();
  98.       }
  99.       
  100.       public function onStateChange(param1:Number) : void
  101.       {
  102.          switch(Number(param1))
  103.          {
  104.             case -1:
  105.                break;
  106.             case 0:
  107.                if(model.config["state"] != ModelStates.BUFFERING && model.config["state"] != ModelStates.IDLE)
  108.                {
  109.                   model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  110.                }
  111.                break;
  112.             case 1:
  113.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  114.                break;
  115.             case 2:
  116.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  117.                break;
  118.             case 3:
  119.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  120.                model.sendEvent(ModelEvent.BUFFER,{"percentage":0});
  121.          }
  122.       }
  123.       
  124.       public function onTimeChange(param1:Number, param2:Number) : void
  125.       {
  126.          model.sendEvent(ModelEvent.TIME,{
  127.             "position":param1,
  128.             "duration":param2
  129.          });
  130.          if(!metasent)
  131.          {
  132.             model.sendEvent(ModelEvent.META,{
  133.                "width":320,
  134.                "height":240,
  135.                "duration":param2
  136.             });
  137.             metasent = true;
  138.          }
  139.       }
  140.       
  141.       public function volume(param1:Number) : void
  142.       {
  143.          outgoing.send("_AS3_to_AS2","setVolume",param1);
  144.       }
  145.       
  146.       private function errorHandler(param1:ErrorEvent) : void
  147.       {
  148.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  149.       }
  150.       
  151.       public function stop() : void
  152.       {
  153.          outgoing.send("_AS3_to_AS2","stopVideo");
  154.       }
  155.       
  156.       public function load() : void
  157.       {
  158.          var _loc1_:* = undefined;
  159.          var _loc2_:* = undefined;
  160.          if(connected)
  161.          {
  162.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  163.             loading = true;
  164.             if(outgoing)
  165.             {
  166.                _loc1_ = getID(model.playlist[model.config["item"]]["file"]);
  167.                _loc2_ = model.playlist[model.config["item"]]["start"];
  168.                outgoing.send("_AS3_to_AS2","loadVideoById",_loc1_,_loc2_);
  169.                model.mediaHandler(loader);
  170.             }
  171.          }
  172.       }
  173.       
  174.       public function onError(param1:String) : void
  175.       {
  176.          var _loc2_:* = undefined;
  177.          _loc2_ = model.playlist[model.config["item"]]["file"];
  178.          model.sendEvent(ModelEvent.ERROR,{"message":"YouTube error (video not found?):\n" + _loc2_});
  179.          stop();
  180.       }
  181.       
  182.       private function getID(param1:String) : String
  183.       {
  184.          var _loc2_:* = undefined;
  185.          var _loc3_:* = undefined;
  186.          var _loc4_:* = undefined;
  187.          _loc2_ = param1.split("?");
  188.          _loc3_ = "";
  189.          for(_loc4_ in _loc2_)
  190.          {
  191.             if(_loc2_[_loc4_].substr(0,2) == "v=")
  192.             {
  193.                _loc3_ = _loc2_[_loc4_].substr(2);
  194.             }
  195.          }
  196.          if(_loc3_ == "")
  197.          {
  198.             _loc3_ = param1.substr(param1.indexOf("/v/") + 3);
  199.          }
  200.          if(_loc3_.indexOf("&") > -1)
  201.          {
  202.             _loc3_ = _loc3_.substr(0,_loc3_.indexOf("&"));
  203.          }
  204.          return _loc3_;
  205.       }
  206.       
  207.       public function play() : void
  208.       {
  209.          outgoing.send("_AS3_to_AS2","playVideo");
  210.       }
  211.       
  212.       public function pause() : void
  213.       {
  214.          outgoing.send("_AS3_to_AS2","pauseVideo");
  215.       }
  216.       
  217.       public function quality(param1:Boolean) : void
  218.       {
  219.       }
  220.    }
  221. }
  222.